package com.woniuxy.lrz.carlproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.dao.ServiceInfoMapper;
import com.woniuxy.lrz.carlproject.model.ServiceInfo;
import com.woniuxy.lrz.carlproject.model.ShopService;
import com.woniuxy.lrz.carlproject.dao.ShopServiceMapper;
import com.woniuxy.lrz.carlproject.service.ShopServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.dto.shopServiceInfoDto;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 门店服务表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
public class ShopServiceServiceImpl extends ServiceImpl<ShopServiceMapper, ShopService> implements ShopServiceService {

    @Resource
    private  ShopServiceMapper shopServiceMapper;
    @Resource
    private ServiceInfoMapper serviceInfoMapper;
    @Override
    @Cacheable(cacheNames = "shopService", key = "#current")
    public Page<ShopService> selectService(int current, int size, Integer shopId) {

         QueryWrapper<ShopService> wrapper = new QueryWrapper<>();
         wrapper.eq("shop_id",shopId);
         Page<ShopService> shopServicePage = shopServiceMapper.selectPage(new Page<>(current, size), wrapper);
        if (shopServicePage == null) {
            throw  new AppException("查询门店服务失败");
        }
        return shopServicePage;
    }

    @Override
    public void addService(Integer serviceId,Integer shopId) {
        //添加之前查询是否存在该服务
        QueryWrapper<ShopService> wrapper = new QueryWrapper<>();
        wrapper.eq("service_id",serviceId);
        ShopService service = shopServiceMapper.selectOne(wrapper);
        if (null !=service){
            throw new AppException("该服务已存在");
        }
        QueryWrapper<ServiceInfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("service_id",serviceId);
        ServiceInfo serviceInfo = serviceInfoMapper.selectOne(wrapper1);
        String serviceName = serviceInfo.getServiceName();
        Double servicePrice = serviceInfo.getServicePrice();
        ShopService shopService = new ShopService();
        shopService.setShopId(shopId);
        shopService.setServiceId(serviceId);
        shopService.setServiceName(serviceName);
        shopService.setServicePrice(servicePrice);
        shopService.setShopServiceStatus("ON");
        int insert = shopServiceMapper.insert(shopService);
        if (insert < 1) {
            throw  new AppException("添加服务失败");
        }
    }

    @Override
    public void deleteService(Integer serviceId, Integer shopId) {
        UpdateWrapper<ShopService> wrapper = new UpdateWrapper<>();
        wrapper.eq("shop_id",shopId)
               .eq("service_id",serviceId);

        int delete = shopServiceMapper.delete(wrapper);
        if (delete < 1) {
            throw  new AppException("删除门店服务失败");
        }
    }

    @Override
    public void updateStatus(Integer serviceId, Integer shopId, String status) {
        UpdateWrapper<ShopService> wrapper = new UpdateWrapper<>();
        wrapper.eq("shop_id",shopId)
                .eq("service_id",serviceId)
                .set("shop_service_status",status);
        int delete = shopServiceMapper.update(null,wrapper);
        if (delete < 1) {
            throw  new AppException("修改门店服务状态失败");
        }
    }

}
